package com.xyz.rok.tools.service.impl;

import com.xyz.rok.tools.mapper.UnionMemberMapper;
import com.xyz.rok.tools.model.UnionMember;
import com.xyz.rok.tools.service.UnionMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UnionMemberServiceImpl implements UnionMemberService {

    @Resource
    private UnionMemberMapper unionMemberMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return unionMemberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(UnionMember record) {
        return unionMemberMapper.insert(record);
    }

    @Override
    public int insertSelective(UnionMember record) {
        return unionMemberMapper.insertSelective(record);
    }

    @Override
    public UnionMember selectByPrimaryKey(Long id) {
        return unionMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(UnionMember record) {
        return unionMemberMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UnionMember record) {
        return unionMemberMapper.updateByPrimaryKey(record);
    }

    @Override
    public int insertOrUpdate(UnionMember unionMember) {
        UnionMember unionMemberDb = unionMemberMapper.selectByGameId(unionMember.getGameId());
        if (unionMemberDb == null) {
            unionMember.setCreateTime(new Date());
            unionMemberMapper.insertSelective(unionMember);
        } else {
            unionMemberDb.setUpdateTime(new Date());
            unionMemberDb.setUnionId(unionMember.getUnionId());
            unionMemberDb.setServerId(unionMember.getServerId());
            unionMemberMapper.updateByPrimaryKeySelective(unionMemberDb);
        }
        return 1;
    }
}
